package com.yanyu.space.common.sentinel;

import com.yanyu.space.common.core.bootlog.BootstrapLogContext;
import com.yanyu.space.common.core.constants.SpaceConstants;
import com.yanyu.space.common.core.yaml.YamlPropertySourceFactory;
import com.yanyu.space.common.sentinel.handler.SentinelGatewayExceptionHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/***
 * @author yanyu
 */
@Configuration
@Slf4j
@PropertySource(factory = YamlPropertySourceFactory.class, value = "classpath:" + SpaceConstants.Properties.SENTINEL_PROP)
@ConditionalOnProperty(name = "spring.cloud.sentinel.enabled", matchIfMissing = true)
public class SentinelAutoConfig {

    public SentinelAutoConfig() {
        BootstrapLogContext.put("sentinel", "开启sentinel功能");
    }

    @Bean
    @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
    public SentinelGatewayExceptionHandler sentinelGatewayExceptionHandler() {
        return new SentinelGatewayExceptionHandler();
    }
}
